/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.controller;

import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdDatabaseModelingConfigSaveBo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelingConfigShowVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.comac.ins.isfd.domain.bo.IsfdDatabaseModelingConfigBo;
import com.comac.ins.isfd.service.IIsfdDatabaseModelingConfigService;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 数据库建模配置
 *
 * @author Lion Li
 * @date 2024-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/databaseModelingConfig")
public class IsfdDatabaseModelingConfigController extends BaseController {

    private final IIsfdDatabaseModelingConfigService isfdDatabaseModelingConfigService;

    /**
     * 查询数据库建模配置列表
     */
    @SaCheckPermission("isfd:databaseModelingConfig:list")
    @PostMapping("/list")
    public R<List<IsfdDatabaseModelingConfigShowVo>> list(@RequestBody IsfdDatabaseModelingConfigBo bo) {
        return R.ok(isfdDatabaseModelingConfigService.queryList(bo));
    }

    /**
     * 新增/修改数据库建模配置
     */
    @SaCheckPermission("isfd:databaseModelingConfig:add")
    @Log(title = "数据库建模配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/save")
    public R<Void> save(@Validated(AddGroup.class) @RequestBody IsfdDatabaseModelingConfigSaveBo saveBo) {
        return toAjax(isfdDatabaseModelingConfigService.saveConfig(saveBo));
    }

    /**
     * 新增数据
     */
    @SaCheckPermission("isfd:databaseModelingConfig:importData")
    @Log(title = "数据导入", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/importData")
    public R<String> importData(@RequestParam("config") String configJson,
                              @RequestParam("file") MultipartFile file) {
        // 将 body 转为对象
        ObjectMapper objectMapper = new ObjectMapper();
        IsfdDatabaseModelingConfigBo bo;
        try {
            bo = objectMapper.readValue(configJson, IsfdDatabaseModelingConfigBo.class);
        } catch (JsonProcessingException e) {
            return R.fail("转换实体类失败！请检查config Json字符串！");
        }
        return R.ok(isfdDatabaseModelingConfigService.importBDFData(bo, file));
    }

    /**
     * 删除数据
     */
    @SaCheckPermission("isfd:databaseModelingConfig:delete")
    @Log(title = "数据导入", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @PostMapping("/delete")
    public R<Integer> delete(@RequestBody IsfdDatabaseModelingConfigBo bo) {
        return R.ok(isfdDatabaseModelingConfigService.deleteConfig(bo));
    }

}
